extends HBoxContainer
var stats:Stats

@onready var health_bar: TextureProgressBar = $VBoxContainer/HealthBar
@onready var eased_health_bar: TextureProgressBar = $VBoxContainer/HealthBar/EasedHealthBar
@onready var energy_bar: TextureProgressBar = $VBoxContainer/EnergyBar

func load(v:Stats) -> void:
	stats = v
	stats.health_changed.connect(update_health)
	update_health(true)
	
	stats.energy_changed.connect(update_energy)
	update_health()
	
	tree_exited.disconnect(func()->void:
		stats.health_changed.disconnect(update_health)
		stats.energy_changed.disconnect(update_energy)
	)

func update_health(skip_anim:bool=false)->void:
	var precentage := float(stats.health) / float(stats.max_health)
	health_bar.value = precentage
	if skip_anim:
		eased_health_bar.value = precentage
	else:
		create_tween().tween_property(eased_health_bar,"value",precentage,0.5)


func update_energy()->void:
	var precentage := stats.energy /stats.max_energy
	energy_bar.value = precentage
